home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11641 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: news.sinet.slb.com!usenet
  2. From: "Vinh D. Nguyen" <vnguyen@sugar-land.anadrill.slb.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help - problem with fstream and random-access files
  5. Date: Fri, 15 Mar 1996 08:54:37 -0600
  6. Organization: Schlumberger Anadrill
  7. Message-ID: <314984AD.388D@sugar-land.anadrill.slb.com>
  8. References: <4i4dnr$ld2@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: 163.185.118.40
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. HAfshar wrote:
  16. > Hello,
  17. > I'm trying to create a random-access file of fixed-length records.  Each
  18. > record (struct) has a couple of integers and a couple of arrays of
  19. > characters.  I initialize the file with zero for each field, since the
  20. > number of records in the file is fixed and I would like to return zero for
  21. > access to a record that has not yet been written.
  22. > I start out writing records to the file and everything seems okay but when
  23. > I try to read back the records and print them to the screen or to a file,
  24. > the first 9 read back unexpected values (but formatted correctly).  After
  25. > that, the 10th record seems to be corrupted, and then the file seems to
  26. > get back in synch again.
  27. > I stepped the program through a debugger and noticed that the get and put
  28. > file pointers seem to be at an unexpected location (for example I expect
  29. > the next record to be at address 0x400 but "tellg" tells me that it is
  30. > actually at 0x402.
  31.  
  32. Does this record corruption and resynching seem to be cyclical, i.e., happening
  33. every 10 records or so? If it does then I suspect that you may have a structure
  34. alignment problem. The best way to get rid of this problem is to read/write
  35. each record in one fell swoop as follows:
  36.  
  37. read( file, &mystruct, sizeof( mystruct ) );
  38. write( file, &mystruct, sizeof( mystruct ) );
  39.  
  40. That is my best guest given the info you posted. If this doesn't fix your problem, 
  41. you will need to provide more info, maybe your structure declaration and some sample code...
  42.  
  43. -- 
  44. --------------------------------------------------------------------------
  45. * Vinh Nguyen                                            vnguyen@slb.com *
  46. * Drilling Information Products - Senior Engineer                        *
  47. * Anadrill Schlumberger                             *
  48. * 200 Gillingham Ln.                             (713) 275-7524 (Office) *
  49. * Sugarland, TX 77478                            (713) 275-8098 (FAX)    *
  50. --------------------------------------------------------------------------
  51.